package com.xpf.controller.center;

import com.xpf.controller.BaseController;
import com.xpf.pojo.Orders;
import com.xpf.pojo.vo.OrderStatusCountsVO;
import com.xpf.service.center.MyOrdersService;
import com.xpf.utils.JSONResult;
import com.xpf.utils.JsonUtils;
import com.xpf.utils.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@Api(value = "用户中心——我的订单",tags = "用户中心——我的订单")
@RestController
@RequestMapping("myorders")
public class MyOrdersController extends BaseController {

//    @Autowired
//    private MyOrdersService myOrdersService;

    @ApiOperation(value = "获得订单状态数", notes = "获得订单状态数")
    @PostMapping("/statusCounts")
    public JSONResult statusCounts(
            @ApiParam(name = "userId",value = "用户id",required = true)
            @RequestParam String userId){

        if (StringUtils.isBlank(userId)){
            return JSONResult.errorMsg(null);
        }
        OrderStatusCountsVO orderStatusCounts = myOrdersService.getOrderStatusCounts(userId);
        return JSONResult.ok(orderStatusCounts);
    }

    @ApiOperation(value = "查询订单列表", notes = "查询订单列表")
    @PostMapping("/query")
    public JSONResult query(
            @ApiParam(name = "userId",value = "用户id",required = true)
            @RequestParam String userId,
            @ApiParam(name = "orderStatus",value = "订单状态",required = false)
            @RequestParam Integer orderStatus,
            @ApiParam(name = "page",value = "当前第几页",required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize",value = "每页条数",required = false)
            @RequestParam Integer pageSize){

        if (StringUtils.isBlank(userId)){
            return JSONResult.errorMsg(null);
        }
        if (page == null)
            page = 1;
        if (pageSize == null)
            pageSize = COMMON_PAGE_SIZE;

        PagedGridResult gridResult = myOrdersService.queryMyOrders(userId, orderStatus, page, pageSize);
        return JSONResult.ok(gridResult);
    }

    @ApiOperation(value = "商家发货", notes = "商家发货")
    @GetMapping("/deliver")
    public JSONResult deliver(
            @ApiParam(name = "orderId",value = "订单号", required = true)
            @RequestParam String orderId){

        if (StringUtils.isBlank(orderId))
            return JSONResult.errorMsg("订单ID不能为空");

        myOrdersService.updateDeliverOrderStatus(orderId);
        return JSONResult.ok();
    }

    @ApiOperation(value = "用户确认收货", notes = "用户确认收货")
    @PostMapping("/confirmReceive")
    public JSONResult confirmReceive(
            @ApiParam(name = "orderId",value = "订单号", required = true)
            @RequestParam String orderId,
            @ApiParam(name = "userId",value = "用户id", required = true)
            @RequestParam String userId){

        //检测用户和订单是否有关联
        JSONResult checkResult = checkUserOrder(userId, orderId);
        if (checkResult.getStatus() != HttpStatus.OK.value()){
            return checkResult;
        }

        boolean result = myOrdersService.updateReceiveOrderStatus(orderId);
        if (!result){
            return JSONResult.errorMsg("订单确认收货失败！");
        }

        return JSONResult.ok();
    }

    @ApiOperation(value = "用户删除订单", notes = "用户删除订单")
    @PostMapping("/delete")
    public JSONResult delete(
            @ApiParam(name = "orderId",value = "订单号", required = true)
            @RequestParam String orderId,
            @ApiParam(name = "userId",value = "用户id", required = true)
            @RequestParam String userId){

        JSONResult checkResult = checkUserOrder(userId, orderId);
        if (checkResult.getStatus() != HttpStatus.OK.value()){
            return checkResult;
        }

        boolean result = myOrdersService.deleteOrder(userId, orderId);
        if (!result){
            return JSONResult.errorMsg("删除订单失败！");
        }

        return JSONResult.ok();
    }

    /**
     * 用于验证用户和订单是否有关联关系，避免非法用户调用
     * @return
     */
//    private JSONResult checkUserOrder(String userId,String orderId){
//        Orders order = myOrdersService.queryMyOrder(userId, orderId);
//        if (order == null){
//            return JSONResult.errorMsg("订单不存在!");
//        }
//        return JSONResult.ok();
//    }

    @ApiOperation(value = "查询订单动向", notes = "查询订单动向")
    @PostMapping("/trend")
    public JSONResult trend(
            @ApiParam(name = "userId",value = "用户id",required = true)
            @RequestParam String userId,
            @ApiParam(name = "page",value = "当前第几页",required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize",value = "每页条数",required = false)
            @RequestParam Integer pageSize){

        if (StringUtils.isBlank(userId)){
            return JSONResult.errorMsg(null);
        }
        if (page == null)
            page = 1;
        if (pageSize == null)
            pageSize = COMMON_PAGE_SIZE;

        PagedGridResult gridResult = myOrdersService.getOrdersTrend(userId,page,pageSize);
        return JSONResult.ok(gridResult);
    }

}
